//
//  QRViewController.swift
//  iWatchManager
//
//  Created by GujyHy on 2018/1/23.
//  Copyright © 2018年 GujyHy. All rights reserved.
//

import UIKit
import swiftScan

class QRViewController: LBXScanViewController {

    // 闪关灯开启状态
    var isOpenedFlash:Bool = false
    //底部显示的功能项
    var bottomItemsView:UIView?
    //闪光灯
    var btnFlash:UIButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "扫一扫"
        view.backgroundColor = UIColor.white
        
//        // 返回
//        let item = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
//        self.navigationItem.backBarButtonItem = item
        
        //需要识别后的图像
        setNeedCodeImage(needCodeImg: true)
        
        //框向上移动10个像素
        scanStyle?.centerUpOffset += 10

    }
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        drawBottomItems()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        for result:LBXScanResult in arrayResult{
            if let str = result.strScanned {
                print(str)
            }
        }
        let result:LBXScanResult = arrayResult[0]
        scanSuccess(result: result)
    }
    
    // MARK: Networking
    func requestData(resultCode:String){
        //let paramer =  ["barcode":"6920177932680"] as [String:Any]!
//        let url = tURL_Server + tURL_getProductOrderDetails
//        let paramer =  ["orderNo":resultCode] as [String:Any]!
//
//        let HUD = MBProgressHUD.showAdded(to: self.view, animated: true)
//        HttpsTools.requestData(.post, URLString:url, parameters:paramer){
//            (result) in
//
//            let json = JSON(result)
//            let code =  json["head"]["code"].stringValue
//            let message =  json["head"]["msg"].stringValue
//            if code == "0000"{
//                HUD.hide(animated: true)
//                var orders:[MyOrderModel] = [MyOrderModel]()
//                let array =  json["body"].arrayValue
//                for sjson:JSON in array {
//                    let order = MyOrderModel.init(sjson)
//                    orders.append(order)
//                }
//                self.pushOrderListViewController(orders: orders)
//            }else{
//                HUD.mode = MBProgressHUDMode.text
//                HUD.detailsLabel.text = ""
//                HUD.label.text = message
//                HUD.margin = 10
//                HUD.offset.y = 50
//                HUD.removeFromSuperViewOnHide = true
//                HUD.hide(animated: true, afterDelay: 1.5)
//                self.perform(#selector(LBXScanViewController.startScan), with: nil, afterDelay: 0.3)
//            }
//        }
    }
    
    func scanSuccess(result:LBXScanResult){
        // 请求商品信息url地址
        print("result.strScanned is  \(result.strScanned!)")
        self.requestData(resultCode: result.strScanned!)
        
    }
//    func pushOrderListViewController(orders:[MyOrderModel]){
//        let vc = OrderListViewController()
//        vc.orders = orders
//        navigationController?.pushViewController(vc, animated: true)
//    }
    
    func drawBottomItems(){
        if (bottomItemsView != nil) {
            return;
        }
        
        let yMax = self.view.frame.maxY - self.view.frame.minY
        bottomItemsView = UIView(frame:CGRect(x: 0.0, y: yMax-100,width: self.view.frame.size.width, height: 100 ) )
        bottomItemsView!.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.6)
        
        self.view .addSubview(bottomItemsView!)
        
        
        let size = CGSize(width: 65, height: 87);
        
        self.btnFlash = UIButton()
        btnFlash.bounds = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        btnFlash.center = CGPoint(x: bottomItemsView!.frame.width/2, y: bottomItemsView!.frame.height/2)
        btnFlash.setImage(UIImage(named: "qrcode_scan_btn_flash_nor"), for:.normal)
        btnFlash.addTarget(self, action: #selector(openOrCloseFlash), for: UIControlEvents.touchUpInside)
        bottomItemsView?.addSubview(btnFlash)
        self.view .addSubview(bottomItemsView!)
    }
    
    
    //开关闪光灯
    @objc func openOrCloseFlash()
    {
        scanObj?.changeTorch();
        
        isOpenedFlash = !isOpenedFlash
        
        if isOpenedFlash
        {
            btnFlash.setImage(UIImage(named: "qrcode_scan_btn_flash_down"), for:UIControlState.normal)
        }
        else
        {
            btnFlash.setImage(UIImage(named: "qrcode_scan_btn_flash_nor"), for:UIControlState.normal)
        }
    }

}
